//
//  HealthLearnDetailsViewController.m
//  Food
//
//  Created by dlios on 15/7/15.
//  Copyright (c) 2015年 李志远. All rights reserved.
//

#import "HealthLearnDetailsViewController.h"

#import <ShareSDK/ShareSDK.h>
#import "UIImageView+WebCache.h"

#import <ShareSDKExtension/SSEShareHelper.h>
#import <ShareSDKUI/ShareSDKUI.h>
#import <ShareSDKUI/SSUIShareActionSheetStyle.h>


@interface HealthLearnDetailsViewController ()<MBProgressHUDDelegate>
@property (nonatomic, retain) UIScrollView *myScrolView;
@property (nonatomic, retain) NSString *htmlStr;
@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) NSString *url;
@property (nonatomic, retain) MBProgressHUD *progress;
@property (nonatomic, assign) BOOL collection;
@property (nonatomic, retain) UIAlertView *alert1;

@end

@implementation HealthLearnDetailsViewController

- (void)dealloc
{
    [super dealloc];
    [_IDStr release];
    [_titleName release];
    [_img release];
    [_myScrolView release];
    [_htmlStr release];
    [_webView release];
    [_url release];
    [_alert1 release];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getData];
   
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"iconfont-share"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(share:)];
    
}

#pragma mark- 收藏

- (void)share:(UIBarButtonItem *)button
{
    NSLog(@"分享");
    
#warning 定制平台分享内容 (用户不会看到你分享的什么内容 无法自定义)
//    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
////    [shareParams SSDKSetupShareParamsByText:@"分享内容"
////                                     images:@[[UIImage imageNamed:@"shareImg.png"]]
////                                        url:[NSURL URLWithString:@"http://mob.com"]
////                                      title:@"分享标题"
////                                       type:SSDKContentTypeImage];
//    
//    [shareParams SSDKSetupSinaWeiboShareParamsByText:[NSString stringWithFormat:@"%@ http://www.xiangha.com/zhishi/%@.html",self.titleName,  self.IDStr]
//                                               title:@"美食坊"
//                                               image:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.img]]]
//                                                 url:nil
//                                            latitude:0
//                                           longitude:0
//                                            objectID:nil
//     
//                                                type:SSDKContentTypeImage];
//    
//    
//    //进行分享
//    [ShareSDK share:SSDKPlatformTypeSinaWeibo
//         parameters:shareParams
//     onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
//         
//         switch (state) {
//             case SSDKResponseStateSuccess:
//             {
//                 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
//                                                                     message:nil
//                                                                    delegate:nil
//                                                           cancelButtonTitle:@"确定"
//                                                           otherButtonTitles:nil];
//                 [alertView show];
//                 break;
//             }
//             case SSDKResponseStateFail:
//             {
//                 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享失败"
//                                                                     message:[NSString stringWithFormat:@"%@", error]
//                                                                    delegate:nil
//                                                           cancelButtonTitle:@"确定"
//                                                           otherButtonTitles:nil];
//                 [alertView show];
//                 break;
//             }
//             case SSDKResponseStateCancel:
//             {
//                 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享已取消"
//                                                                     message:nil
//                                                                    delegate:nil
//                                                           cancelButtonTitle:@"确定"
//                                                           otherButtonTitles:nil];
//                 [alertView show];
//                 break;
//             }
//             default:
//                 break;
//         }
//         
//        
//         
//     }];
//
    
    Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    if (r.currentReachabilityStatus == NotReachable) {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil];
        [alert1 show];
        [alert1 release];
        
    }else{

#warning 显示内容编辑界面
    //创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"%@ http://www.xiangha.com/zhishi/%@.html",self.titleName, self.IDStr]

                                     images:@[[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.img]]]]
                                        url:nil
                                      title:@"美食坊"
                                       type:SSDKContentTypeImage];
    
    
    
    [ShareSDK showShareEditor:SSDKPlatformTypeSinaWeibo
           otherPlatformTypes:@[@(SSDKPlatformTypeTencentWeibo), @(SSDKPlatformTypeWechat)]
                  shareParams:shareParams
          onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
              
              switch (state) {
                      
                  case SSDKResponseStateBegin:
                  {
                      break;
                  }
                  case SSDKResponseStateSuccess:
                  {
                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                          message:nil
                                                                         delegate:nil
                                                                cancelButtonTitle:@"确定"
                                                                otherButtonTitles:nil];
                      [alertView show];
                      break;
                  }
                  case SSDKResponseStateFail:
                  {
                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                          message:[NSString stringWithFormat:@"%@", error]
                                                                         delegate:nil
                                                                cancelButtonTitle:@"确定"
                                                                otherButtonTitles:nil];
                      [alertView show];
                      break;
                  }
                  case SSDKResponseStateCancel:
                  {
                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享已取消"
                                                                          message:nil
                                                                         delegate:nil
                                                                cancelButtonTitle:@"确定"
                                                                otherButtonTitles:nil];
                      [alertView show];
                      break;
                  }
                  default:
                      break;
              }
              
              if (state != SSDKResponseStateBegin)
              {
                               }
              
          }];

    }
}


- (void)createProgress
{
    self.progress = [[MBProgressHUD alloc] initWithView:self.view];
    [self.progress show:YES];
    self.progress.labelText = @"稍等";
    self.progress.detailsLabelText = @"正在加载...";
    [self.view addSubview:self.progress];
}

- (void)hudWasHidden:(MBProgressHUD *)hud
{
    [_progress removeFromSuperview];
    [_progress release];
}

- (void)getData
{
    Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    if (r.currentReachabilityStatus == NotReachable) {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil];
        [alert1 show];
        [alert1 release];
    }else{

    
    self.url = [NSString stringWithFormat:@"%@%@",HEAITHKNOWDATAILS,self.IDStr];
    [AFNet connectNetGetDataWithURLString:self.url parameters:nil finishblock:^(id result) {
        [self.progress hide:YES];
       self.htmlStr = [[result objectForKey:@"data"] objectForKey:@"html"];
        [self createView];
    }];
         [self createProgress];
    }
}

- (void)createView
{
    self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    self.webView.backgroundColor = [UIColor whiteColor];
    [self.webView loadHTMLString:self.htmlStr baseURL:[NSURL URLWithString:self.url]];
    [self.view addSubview:self.webView];
    [_webView release];
}
- (void)data{
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
